<template>
  <div class="login-containter">
    <el-row>
      <el-col :span="14" :offset="0" :xs="0" />
      <el-col :span="8" :offset="0" :xs="24">
        <el-form ref="loginFormRef" class="login-form" :model="loginForm" :rules="rules">
          <div class="form-top">
            <h1>Hello</h1>
            <p>欢迎来到阿森甄选</p>
          </div>
          <div class="form-bottom">
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" placeholder="请输入用户名" :prefix-icon="User" />
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" placeholder="请输入密码" :show-password="true" :prefix-icon="Lock" />
            </el-form-item>
            <el-form-item>
              <el-button :loading="loading" class="login-botton" type="primary" size="default" @click="login">登录</el-button>
            </el-form-item>
          </div>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import type { loginForm } from '@/api/user/type';
import type { FormRules } from 'element-plus';

import { reactive, ref } from 'vue';
import { User, Lock } from '@element-plus/icons-vue';
import useUserStore from '@/store/modules/user';
import { ElNotification } from 'element-plus';
import { type Router, useRouter, useRoute } from 'vue-router';
import { getTime } from '@/utils/time';

let userStore = useUserStore();

let $router: Router = useRouter();
let $route = useRoute();

let loginForm = reactive<loginForm>({
  username: 'admin',
  password: '111111',
});

let loading = ref<boolean>(false);
const login = async () => {
  if (loading.value) return;
  loading.value = true;
  try {
    await loginFormRef.value.validate();
    await userStore.userLogin(loginForm);
    let redirect: any = $route.query.redirect;
    $router.push({ path: redirect || '/' });

    ElNotification({
      type: 'success',
      title: `Hi,${getTime()}好!`,
      message: '欢迎回来',
    });
  } catch (error) {
    ElNotification({
      type: 'error',
      title: '登录失败',
      message: (error as Error).message || '账号密码不合规',
    });
  }
  loading.value = false;
};

const validatorUsername = (_rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback();
  } else {
    callback(new Error('账号长度至少为5位'));
  }
};

const validatorPassword = (_rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback();
  } else {
    callback(new Error('密码长度至少为5位'));
  }
};

const rules = reactive<FormRules<loginForm>>({
  username: [
    {
      validator: validatorUsername,
      trigger: 'change',
    },
  ],
  password: [
    {
      validator: validatorPassword,
      trigger: 'change',
    },
  ],
});

let loginFormRef = ref();
</script>

<style lang="scss" scoped>
.login-containter {
  width: 100%;
  height: 100vh;
  background: url(/src/assets/images/background.jpg);
  .login-form {
    margin-top: 50vh;
    margin-left: 50%;
    transform: translate(-50%, -50%);
    width: 70%;
    background: rgba(255, 255, 255, 0.1); /* 半透明白色背景 */
    backdrop-filter: blur(10px); /* 模糊效果 */
    -webkit-backdrop-filter: blur(10px); /* 兼容 WebKit 浏览器 */
    border-radius: 5px; /* 可选：圆角 */
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* 可选：阴影效果 */
    padding: 60px 10%;
    box-sizing: border-box;
    .form-top {
      margin-bottom: 30px;
      color: #ffffff;
      display: flex;
      flex-direction: column;
      gap: 10px;
    }
    .form-bottom {
      .login-botton {
        width: 100%;
      }
    }
  }
}
</style>
